From ae6780b241d8e6f389d5c25e4cac34a02e22a459 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Wed, 14 Mar 2007 20:41:56 +0000 Subject: [PATCH] Implement VM_metrics Xen-API class. Signed-off-by: Tom Wilkie Signed-off-by: Ewan Mellor --- tools/python/xen/xend/XendAPI.py | 55 ++++++++++++++++----- tools/python/xen/xend/XendDomainInfo.py | 10 ++++ tools/python/xen/xend/XendVMMetrics.py | 65 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 tools/python/xen/xend/XendVMMetrics.py diff --git a/tools/python/xen/xend/XendAPI.py b/tools/python/xen/xend/XendAPI.py index 2c451b2b92..c1b13cb595 100644 --- a/tools/python/xen/xend/XendAPI.py +++ b/tools/python/xen/xend/XendAPI.py @@ -31,6 +31,7 @@ from xen.xend.XendError import * from xen.xend.XendClient import ERROR_INVALID_DOMAIN from xen.xend.XendLogging import log from xen.xend.XendTask import XendTask +from xen.xend.XendVMMetrics import XendVMMetrics from xen.xend.XendAPIConstants import * from xen.util.xmlrpclib2 import stringify @@ -192,6 +193,16 @@ def valid_vm(func): _check_ref(XendDomain.instance().is_valid_vm, 'VM', func, *args, **kwargs) +def valid_vm_metrics(func): + """Decorator to verify if vm_metrics_ref is valid before calling method. + + @param func: function with params: (self, session, vm_metrics_ref, ...) + @rtype: callable object + """ + return lambda *args, **kwargs: \ + _check_ref(XendVMMetrics.is_valid_vm_metrics, + 'VM_metrics', func, *args, **kwargs) + def valid_network(func): """Decorator to verify if network_ref is valid before calling method. @@ -400,6 +411,7 @@ class XendAPI(object): 'host_metrics' : valid_host_metrics, 'network' : valid_network, 'VM' : valid_vm, + 'VM_metrics' : valid_vm_metrics, 'VBD' : valid_vbd, 'VBD_metrics' : valid_vbd_metrics, 'VIF' : valid_vif, @@ -990,11 +1002,9 @@ class XendAPI(object): VM_attr_ro = ['power_state', 'resident_on', - 'memory_actual', 'memory_static_max', 'memory_static_min', 'VCPUs_number', - 'VCPUs_utilisation', 'consoles', 'VIFs', 'VBDs', @@ -1002,6 +1012,7 @@ class XendAPI(object): 'tools_version', 'domid', 'is_control_domain', + 'metrics' ] VM_attr_rw = ['name_label', @@ -1098,11 +1109,7 @@ class XendAPI(object): def VM_get_resident_on(self, session, vm_ref): return xen_api_success(XendNode.instance().uuid) - - def VM_get_memory_actual(self, session, vm_ref): - dom = XendDomain.instance().get_vm_by_uuid(vm_ref) - return xen_api_todo() # unsupported by xc - + def VM_get_memory_static_max(self, session, vm_ref): dom = XendDomain.instance().get_vm_by_uuid(vm_ref) return xen_api_success(dom.get_memory_static_max()) @@ -1115,10 +1122,6 @@ class XendAPI(object): dom = XendDomain.instance().get_vm_by_uuid(vm_ref) return xen_api_success(dom.getVCpuCount()) - def VM_get_VCPUs_utilisation(self, session, vm_ref): - dom = XendDomain.instance().get_vm_by_uuid(vm_ref) - return xen_api_success(dom.get_vcpus_util()) - def VM_get_VIFs(self, session, vm_ref): dom = XendDomain.instance().get_vm_by_uuid(vm_ref) return xen_api_success(dom.get_vifs()) @@ -1139,6 +1142,10 @@ class XendAPI(object): dom = XendDomain.instance().get_vm_by_uuid(vm_ref) return dom.get_tools_version() + def VM_get_metrics(self, _, vm_ref): + dom = XendDomain.instance().get_vm_by_uuid(vm_ref) + return xen_api_success(dom.get_metrics()) + # attributes (rw) def VM_get_name_label(self, session, vm_ref): dom = XendDomain.instance().get_vm_by_uuid(vm_ref) @@ -1418,11 +1425,9 @@ class XendAPI(object): 'memory_static_max': xeninfo.get_memory_static_max(), 'memory_dynamic_min': xeninfo.get_memory_dynamic_min(), 'memory_dynamic_max': xeninfo.get_memory_dynamic_max(), - 'memory_actual': xeninfo.get_memory_static_min(), 'VCPUs_policy': xeninfo.get_vcpus_policy(), 'VCPUs_params': xeninfo.get_vcpus_params(), 'VCPUs_number': xeninfo.getVCpuCount(), - 'VCPUs_utilisation': xeninfo.get_vcpus_util(), 'actions_after_shutdown': xeninfo.get_on_shutdown(), 'actions_after_reboot': xeninfo.get_on_reboot(), 'actions_after_suspend': xeninfo.get_on_suspend(), @@ -1497,6 +1502,30 @@ class XendAPI(object): return XendTask.log_progress(0, 100, do_vm_func, "domain_unpause", vm_ref) + # Xen API: Class VM_metrics + # ---------------------------------------------------------------- + + VM_metrics_attr_ro = ['memory_actual', + 'vcpus_number', + 'vcpus_utilisation'] + VM_metrics_attr_rw = [] + VM_metrics_methods = [] + + def _VM_metrics_get(self, ref): + return XendVMMetrics.get_by_uuid(ref) + + def VM_metrics_get_record(self, _, ref): + return xen_api_success(self._VM_metrics_get(ref).get_record()) + + def VM_metrics_get_memory_actual(self, _, ref): + return xen_api_success(self._VM_metrics_get(ref).get_memory_actual()) + + def VM_metrics_get_vcpus_number(self, _, ref): + return xen_api_success(self._VM_metrics_get(ref).get_vcpus_number()) + + def VM_metrics_get_vcpus_utilisation(self, _, ref): + return xen_api_success(self._VM_metrics_get(ref).get_metrics_get_vcpus_utilisation()) + # Xen API: Class VBD # ---------------------------------------------------------------- diff --git a/tools/python/xen/xend/XendDomainInfo.py b/tools/python/xen/xend/XendDomainInfo.py index e4f30fc6e4..bec2c5ad2c 100644 --- a/tools/python/xen/xend/XendDomainInfo.py +++ b/tools/python/xen/xend/XendDomainInfo.py @@ -51,6 +51,8 @@ from xen.xend.xenstore.xswatch import xswatch from xen.xend.XendConstants import * from xen.xend.XendAPIConstants import * +from xen.xend.XendVMMetrics import XendVMMetrics + MIGRATE_TIMEOUT = 30.0 BOOTLOADER_LOOPBACK_DEVICE = '/dev/xvdp' @@ -369,6 +371,8 @@ class XendDomainInfo: self._augmentInfo(priv) self._checkName(self.info['name_label']) + + self.metrics = XendVMMetrics(uuid.createString(), self) # @@ -627,6 +631,10 @@ class XendDomainInfo: except RuntimeError, exn: raise XendError(str(exn)) + + def getDomInfo(self): + return dom_get(self.domid) + # # internal functions ... TODO: re-categorised # @@ -2060,6 +2068,8 @@ class XendDomainInfo: return self.info.get('pci_bus', '') def get_tools_version(self): return self.info.get('tools_version', {}) + def get_metrics(self): + return self.metrics.get_uuid(); def get_on_shutdown(self): after_shutdown = self.info.get('actions_after_shutdown') diff --git a/tools/python/xen/xend/XendVMMetrics.py b/tools/python/xen/xend/XendVMMetrics.py new file mode 100644 index 0000000000..8589e35171 --- /dev/null +++ b/tools/python/xen/xend/XendVMMetrics.py @@ -0,0 +1,65 @@ +#============================================================================ +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (c) 2006-2007 Xensource Inc. +#============================================================================ + +from xen.xend.XendLogging import log + +instances = {} + +class XendVMMetrics: + """VM Metrics.""" + + def get_by_uuid(_, uuid): + return instances[uuid] + + get_by_uuid = classmethod(get_by_uuid) + + def is_valid_vm_metrics(_, uuid): + return uuid in instances + + is_valid_vm_metrics = classmethod(is_valid_vm_metrics) + + def __init__(self, uuid, xend_domain_instance): + self.uuid = uuid + self.xend_domain_instance = xend_domain_instance + instances[uuid] = self + + def get_uuid(self): + return self.uuid + + def get_memory_actual(self): + return self.get_record()["memory_actual"] + + def get_vcpus_number(self): + return self.get_record()["vcpus_number"] + + def get_vcpus_utilisation(self): + return self.xend_domain_instance.get_vcpus_util() + + def get_record(self): + domInfo = self.xend_domain_instance.getDomInfo() + if domInfo: + return { 'uuid' : self.uuid, + 'memory_actual' : domInfo["mem_kb"] * 1024, + 'vcpus_number' : domInfo["online_vcpus"], + 'vcpus_utilisation' : self.get_vcpus_utilisation() + } + else: + return { 'uuid' : self.uuid, + 'memory_actual' : 0, + 'vcpus_number' : 0, + 'vcpus_utilisation' : {} + } -- 2.30.2